package com.songlanyun.modules.share.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.songlanyun.common.enums.OpusConstant;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.browse.entity.BrowseRecord;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.share.dao.ShareRecordDao;
import com.songlanyun.modules.share.entity.ShareRecord;
import com.songlanyun.modules.share.service.ShareRecordService;


@Service("reprintRecordService")
public class ShareRecordServiceImpl extends ServiceImpl<ShareRecordDao, ShareRecord> implements ShareRecordService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ShareRecord> page = this.page(
                new Query<ShareRecord>().getPage(params),
                new QueryWrapper<ShareRecord>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存分享记录
     *
     * @param modelType
     * @param modelId
     * @param shareUserId
     * @param publisherId
     */
    @Override
    public Boolean share(OpusConstant.ModelType modelType, Long modelId, Long shareUserId, Long publisherId, Date time) {
        //一个用户对同一类型的分享只记录一次
        ShareRecord record = this.baseMapper.selectOne(
                new LambdaQueryWrapper<ShareRecord>()
                        .eq(ShareRecord::getModelType, modelType)
                        .eq(ShareRecord::getModelId, modelId)
                        .eq(ShareRecord::getShareUserId, shareUserId)
                        .last("limit 1")
        );

        if (record == null) {
            record = new ShareRecord(modelType, modelId, shareUserId, publisherId, time);
            ValidatorUtils.validateEntity(record);
            this.baseMapper.insert(record);
            return true;
        } else {
            record.setLastTime(time);
            this.baseMapper.updateById(record);
            return false;
        }


    }

    @Override
    public void modelDel(OpusConstant.ModelType modelType, Long modelId) {
        this.update(
                new LambdaUpdateWrapper<ShareRecord>()
                        .eq(ShareRecord::getModelType, modelType)
                        .eq(ShareRecord::getModelId, modelId)
                        .set(ShareRecord::getModelExist, false)
        );
    }

    private Boolean ifShare(OpusConstant.ModelType modelType, Long modelId, Long shareUserId) {
        Integer count = this.baseMapper.selectCount(
                new LambdaQueryWrapper<ShareRecord>()
                        .eq(ShareRecord::getModelType, modelType)
                        .eq(ShareRecord::getModelId, modelId)
                        .eq(ShareRecord::getShareUserId, shareUserId)
        );

        return count > 0;
    }

}